//
//  WmcHomePhotoLayoutView.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/8/3.
//

import UIKit

class WmcHomePhotoLayoutView: WmcBaseView {

    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.spacing = 8.0
        stackV.distribution = .fill
        stackV.alignment = .fill
        return stackV
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = 15.wmc_scaleFont(weight: .medium)
        label.textColor = UIColor.wmc_white_FFFFFF
        label.text = "连续拍照合成一张"
        return label
    }()
    private lazy var layoutContainerView: UIView = {
        let view = UIView()
        return view
    }()
    private lazy var layoutView: UIView = {
        let view = UIView()
//        view.backgroundColor = UIColor.wmc_white_FFFFFF
        return view
    }()
    
    private lazy var layoutStackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.spacing = 0.0
        stackV.distribution = .fill
        stackV.alignment = .fill
        return stackV
    }()
    
    private lazy var imageView: UIImageView = {
        let imgView = UIImageView()
        imgView.backgroundColor = UIColor.wmc_white_FFFFFF
        imgView.image = image
        imgView.contentMode = .scaleAspectFit
        return imgView
    }()
    
    private lazy var gridView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }()
    
    private let spacing = 3.0.wmc_autoHor
    private let itemWH = 12.0.wmc_autoHor
    private let image = "icon_work_report_78_30".wmc_image
    
    /// 列数
    var columns: Int = 1
    /// 照片总张数
    var totalCount: Int = 2
    /// 已拍照片张数
    var finishCount: Int = 0
    /// 拼图模板风格
    var style: WmcPhotoTemplate.Style = .default {
        didSet {
            switch style {
            case .default:
                imageView.isHidden = true
            case .workReport:
                imageView.isHidden = false
            }
        }
    }
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        backgroundColor = UIColor.clear
        // stackView
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        // layoutContainerView
        stackView.addArrangedSubview(layoutContainerView)
        // layoutView
        layoutContainerView.addSubview(layoutView)
        layoutView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        // layoutStackView
        layoutView.addSubview(layoutStackView)
        layoutStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        // imageView
        layoutStackView.addArrangedSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.height.equalTo(13.wmc_autoHor)
        }
        // gridView
        layoutStackView.addArrangedSubview(gridView)
        gridView.snp.makeConstraints { make in
            make.width.equalTo(0.0)
            make.height.equalTo(0.0)
        }
        // titleLabel
        stackView.addArrangedSubview(titleLabel)
    }
    
}

extension WmcHomePhotoLayoutView {
    
    func reload() {
        // 计算总行数
        let rows = (totalCount - 1) / columns + 1
        // 总共的行间距
        let total_row_space = (rows + 1).yp.cgFloat * spacing
        // gridView的高度
        let gridH = rows.yp.cgFloat * itemWH + total_row_space
        // 总共的列间距
        let total_col_space = (columns + 1).yp.cgFloat * spacing
        // gridView的宽度
        let gridW = columns.yp.cgFloat * itemWH + total_col_space
        // 更新gridView的高度
        gridView.snp.updateConstraints { make in
            make.width.equalTo(gridW)
            make.height.equalTo(gridH)
        }
        let imageH = gridW * image.size.height / image.size.width
        imageView.snp.updateConstraints { make in
            make.height.equalTo(imageH)
        }
        // 布局子控件
        gridView.subviews.forEach { $0.removeFromSuperview() }
        for index in 0..<totalCount {
            let itemView = UIView()
            itemView.backgroundColor = index < finishCount ? UIColor.wmc_mainTheme : UIColor.clear
            itemView.layer.borderColor = UIColor.wmc_white_FFFFFF.cgColor
            itemView.layer.borderWidth = spacing
            gridView.addSubview(itemView)
            let row = index / columns
            let column = index % columns
            let x = (itemWH + spacing) * column.yp.cgFloat
            let y = (itemWH + spacing) * row.yp.cgFloat
            let size = spacing * 2.0 + itemWH
            itemView.frame = CGRect(x: x, y: y, width: size, height: size)
        }
    }
    
    
}
